﻿package net.pezzato.genetree{
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.display.Stage;
	import net.pezzato.genetree.graphics.NodeWidget;
	import net.pezzato.genetree.info.GeneNode;
	import net.pezzato.genetree.graphics.SelectIdWindow;
	import net.pezzato.genetree.graphics.Sky;
	import net.pezzato.localization.Translator;
	import net.pezzato.localization.TranslatorLoader;
	import net.pezzato.graphics.WindowManager;
	public class GeneTree extends WindowManager {
		private static  const DEFAULT_XML_PREFIX:String = "./id-";
		private static  const DEFAULT_XML_SUFFIX:String = ".xml";
		private static  const DEFAULT_LANG:String = "it_IT";
		private var currentNodeId_:String;
		private var xmlPrefix_:String;
		private var xmlSuffix_:String;
		private var lang_:String;
		private var sky_:Sky;
		private var askNodeWindow_:SelectIdWindow;
		public function GeneTree() {
			desktop.expand(stage.stageWidth, stage.stageHeight);
			readOptions(root.loaderInfo.parameters);
		}
		private function onLoadTreeComplete(event:Event):void {
			var xml:XML = new XML(event.target.data);
			var node:GeneNode = new GeneNode(xml);
			sky_ = new Sky(desktop);
			var startNw:NodeWidget = sky_.startTreeFrom(node); 
			sky_.centerStartNode();
			if (xml.@titleTag != undefined) {
				startNw.nodeTitle = xml.@titleTag;
			}
		}
		private function askForNodeId():void {
			askNodeWindow_ = new SelectIdWindow(desktop);
			askNodeWindow_.addEventListener(SelectIdWindow.ID_SELECTED, loadTree);
			askNodeWindow_.centerWindow();
		}
		private function readOptions(options:Object) {
			xmlPrefix_ = options.xmlPrefix;
			if (xmlPrefix_ == null) {
				xmlPrefix_ = DEFAULT_XML_PREFIX;
			}
			xmlSuffix_ = options.xmlSuffix;
			if (xmlSuffix_ == null) {
				xmlSuffix_ = DEFAULT_XML_SUFFIX;
			}
			lang_ = options.lang;
			if (lang_ == null) {
				lang_ = DEFAULT_LANG;
			}
			Translator.loader.addEventListener(TranslatorLoader.READY, onTranslatorReady);
			Translator.loadLang(lang_);
			//currentNodeId_ = options.nodeId; /* commenta per test */
			currentNodeId_ = "00000001"; /* decommenta per test */
		}
		private function onTranslatorReady(event:Event):void {
			if (currentNodeId_ == null) {
				askForNodeId();
			} else {
				loadTree();
			}
		}
		private function loadTree(event:Event = null):void {
			if (event != null) {
				if (event.target.selectedId != null) {
					currentNodeId_ = event.target.selectedId;
				}
			}
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest(xmlPrefix_ + currentNodeId_ + xmlSuffix_);
			loader.addEventListener(Event.COMPLETE, onLoadTreeComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadTreeError);
			loader.load(request);
		}
		private function onLoadTreeError(event:IOErrorEvent) {
			trace(event);
			alert(Translator.translate("ioError"));
		}
	}
}